home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_360 / uucp / uucp0.lzh / src / GUtil / uuserdump.c < prev    next >
C/C++ Source or Header  |  1990-05-28  |  4KB  |  160 lines

  1.  
  2. /*
  3.  *  UUSERDUMP.C
  4.  *
  5.  *  $Header: Beta:src/uucp/src/GUtil/RCS/uuserdump.c,v 1.1 90/02/02 11:58:57 dillon Exp Locker: dillon $
  6.  *
  7.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  8.  *
  9.  *  UUSERDUMP filename
  10.  *
  11.  *  KEY THINGS ABOUT UUSER: USED AS STDIN/STDOUT
  12.  *
  13.  *    Currently UUSER: works only with single sessions started
  14.  *    by Getty (this has to do with how UUSER: deals with carrier
  15.  *    detect).  THIS WILL BE FIXED.
  16.  *
  17.  *    (1) Use Level 1 I/O if you can (read/write)
  18.  *        read() returns 0 on timeout, -1 on carrier lost
  19.  *        otherwise, read() returns the immediate number of
  20.  *        data bytes ready up to the amount you requested.
  21.  *        (i.e. if you read(0,buf,256) you can get anywhere from
  22.  *        -1, 0, 1 to 256 back).
  23.  *
  24.  *        write() returns the number you wrote or -1 (lost carrier)
  25.  *
  26.  *        To 'poll' data ready you can read(0, NULL, 0) .. reading
  27.  *        0 bytes returns 0 (data ready) or -1 (data not ready).
  28.  *        NOTE: 0 (data ready) will be returned if carrier is lost
  29.  *        when you read 0 bytes... this is so your program thinks
  30.  *        data is ready and when it does a real read it finds that
  31.  *        carrier was lost!
  32.  *
  33.  *    (2) If you want to use Level 2 I/O (stdio) I suggest you use
  34.  *        it for writing only.  If you really want to use it for
  35.  *        reading remember that the timeout will cause an EOF
  36.  *        condition which must be cleared (clrerr()).  And you
  37.  *        must call ferror() to determine whether an EOF is an
  38.  *        EOF (timeout()) or an actual error (lost carrier).
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include "protos.h"
  43. #include "version.h"
  44.  
  45. IDENT(".01");
  46.  
  47. char buf[256];
  48. short i;
  49.  
  50. int
  51. brk()
  52. {
  53.     return(0);
  54. }
  55.  
  56. main(ac, av)
  57. char *av[];
  58. {
  59.     short i;
  60.     char c;
  61.     short n;
  62.     FILE *fi = fopen(av[1], "r");
  63.  
  64.     onbreak(brk);
  65.     /*
  66.      *    set output to line buffering (stdio), else it will use
  67.      *    file buffering!  Which is bad if we want to catch ^S/^C
  68.      */
  69.  
  70.     setvbuf(stdout, NULL, _IOLBF, 0);
  71.  
  72.  
  73.     if (ac == 1)
  74.     exit(1);
  75.  
  76.     if (fi == NULL)
  77.     printf("Info file %s not available\r\n", av[1]);
  78.  
  79.     printf("Dumping info file (suggest capture), hit Enter when ready\n");
  80.     fflush(stdout);
  81.  
  82.     /*
  83.      *    Getty uses the R1000 option, so there is a one second timeout
  84.      *    on reads when no data is present.  If data is present, UUSER:
  85.      *    returns the number of bytes immediately available regardless of
  86.      *    how many you requested.
  87.      */
  88.  
  89.     for (i = 0; i < 60; ++i) {
  90.     n = read(0, &c, 1);
  91.     if (n < 0)              /*  lost carrier */
  92.         exit(1);
  93.                 /*  success    */
  94.     if (n > 0 && (c == 10 || c == 13))
  95.         goto skip;
  96.     /* timeout */
  97.     }
  98.     exit(1);
  99. skip:
  100.     while (fgets(buf, 256, fi)) {
  101.     short len = strlen(buf);
  102.     if (CheckControl())
  103.         break;
  104.     if (len > 0 && buf[len-1] == '\n')
  105.         --len;
  106.     buf[len++] = 13;
  107.     buf[len++] = 10;
  108.     buf[len++] = 0;
  109.     fputs(buf, stdout);
  110.     }
  111.     fclose(fi);
  112.  
  113.     printf("End of dump, any key to disconnect\n");
  114.     fflush(stdout);
  115.     for (i = 0; i < 60; ++i) {
  116.     n = read(0, &c, 1);
  117.     if (n == 0)     /*  1 second timeout    */
  118.         continue;
  119.     break;        /*  any key or lost cd    */
  120.     }
  121.     return(0);
  122. }
  123.  
  124. /*
  125.  *  Here we use a UUSER: trick ... requesting 0 bytes in a read()
  126.  *  incurs NO timeout.    0 is returned if data is ready, -1 if data
  127.  *  is not ready.
  128.  *
  129.  *  Another way to do it is to dump a seconds worth of data, then
  130.  *  call read() (which blocks for up to 1 second if no data is
  131.  *  ready, but the user doesn't see that because we've already queued
  132.  *  a second's worth of data).  The first method is better because
  133.  *  there is finer granularity (that is, the one used below)
  134.  */
  135.  
  136. CheckControl()
  137. {
  138.     char c = 0;
  139.     short n;
  140.  
  141.     if (read(0, NULL, 0) == 0) {    /*  returns 0=data ready, -1=not rdy */
  142.     read(0, &c, 1);
  143.     if (c == ('s'&0x1F)) {      /*  ^S           */
  144.         for (;;) {
  145.         n = read(0, &c, 1);
  146.         if (n < 0)          /*  lost carrier */
  147.             exit(1);
  148.         if (n == 0)         /*  timeout      */
  149.             continue;
  150.         break;            /*    got a key    */
  151.         }
  152.         return(0);
  153.     }
  154.     if (c == ('c'&0x1F))        /*  ^C           */
  155.         return(1);
  156.     }
  157.     return(0);
  158. }
  159.  
  160.